package forum.dao;

import forum.model.User;
import forum.util.DBUtil;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author haomin
 * @date 2022/06/29 17:22
 **/

// 从数据库查找user数据的方法类
public class UserDao {

    // 用户帖子数
    @SneakyThrows
    public int selectPointCountByUid(int uid){
        String sql = "select count(*) from topic where uID = ?";
        try (Connection c = DBUtil.connection()){
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);
                try (ResultSet rs = ps.executeQuery()){
                    rs.next();
                    int count = rs.getInt("count(*)");
                    updatePointCountByUid(uid,count);
                    return count;
                }
            }
        }
    }

    // 更新主贴回复数
    @SneakyThrows
    public void updatePointCountByUid(int uID, int count) {
        String sql = "update user set userPoint = ? where uID = ?";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1,count);
                ps.setInt(2,uID);

                ps.executeUpdate();
            }
        }
    }

    // 用户等级
    public int selectClassCountByUid(int uid){
        int point = new UserDao().selectPointCountByUid(uid);
        int classCount = 0;
        if(point == 0){
            classCount = 0;
        }else if(point < 5){
            classCount = 1;
        }else if(point < 15){
            classCount = 2;
        }else if(point < 30){
            classCount = 3;
        }else {
            classCount = 4;
        }
        updateClassCountByUid(uid,classCount);
        return classCount;
    }

    @SneakyThrows
    private void updateClassCountByUid(int uID, int classCount) {
        String sql = "update user set userClass = ? where uID = ?";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1,classCount);
                ps.setInt(2,uID);

                ps.executeUpdate();
            }
        }

    }

    // 查询用户ByUsernameAndPassword
    @SneakyThrows
    public User selectOneByUsernameAndPassword(String username,String password){
        String sql = "select uID, userSex, userAge,userOccupation, userHobby, userPoint, userClass, userRegister from user where userName = ? and userPassword = ?";
        try (Connection c = DBUtil.connection()){
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setString(1,username);
                ps.setString(2,password);

                System.out.println("DEBUG: " + ps);
                try (ResultSet rs = ps.executeQuery()){
                    if(!rs.next()){
                        return null;
                    }

                    User user = new User();
                    user.uID = rs.getInt("uID");
                    user.username = username;
                    user.password = password;
                    user.userSex = rs.getString("userSex");
                    user.userAge = rs.getInt("userAge");
                    user.userOccupation = rs.getString("userOccupation");
                    user.userHobby = rs.getString("userHobby");
                    user.userPoint = rs.getInt("userPoint");
                    user.userClass = rs.getInt("userClass");
                    String Time = rs.getString("userRegister");
                    user.userRegister = Time.substring(0,Time.length()-5);

                    return user;
                }
            }
        }
    }

    // 查询用户名ByUid
    @SneakyThrows
    public String selectUserNameByUid(int uid){
        String sql = "select username from user where uID = ?";
        try (Connection c = DBUtil.connection()){
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);
                try (ResultSet rs = ps.executeQuery()){
                    if(!rs.next()){
                        return null;
                    }
                    String username = rs.getString("username");

                    return username;
                }
            }
        }
    }

    // 新建用户
    @SneakyThrows
    public User insert(String username, String password, String sex, int age, String occupation, String hobby) {
        String sql = "insert into user (username,userPassword,userSex,userAge,userOccupation,userHobby) values (?, ?, ?, ?, ?, ?)";
        try (Connection c = DBUtil.connection()){
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setString(1,username);
                ps.setString(2,password);
                ps.setString(3,sex);
                ps.setInt(4,age);
                ps.setString(5,occupation);
                ps.setString(6,hobby);

                ps.executeUpdate();
                System.out.println("新建用户完成");
                return new UserDao().selectOneByUsernameAndPassword(username,password);
            }
        }
    }
}